import React, { Suspense, lazy } from 'react';
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom';
import PageLoader from '../components/PageLoader';
import ErrorBoundary from '../components/ErrorBoundary';

// 路由级按需加载
const Login = lazy(() => import('../pages/Login/Login'));
const BasicLayout = lazy(() => import('../layouts/BasicLayout'));
const Dashboard = lazy(() => import('../pages/Dashboard/Dashboard'));
const Dashboard3DPage = lazy(() => import('../pages/Dashboard3D/Dashboard3D'));
const Alarm = lazy(() => import('../pages/Alarm'));
const Profile = lazy(() => import('../pages/Profile'));
const Settings = lazy(() => import('../pages/Settings'));
const NotFound = lazy(() => import('../pages/NotFound/NotFound'));
const EquipmentList = lazy(() => import('../pages/Equipment/EquipmentList'));
const EquipmentMonitor = lazy(() => import('../pages/Equipment/EquipmentMonitor'));
const StatisticsOverview = lazy(() => import('../pages/Statistics/StatisticsOverview'));
const ReportCenter = lazy(() => import('../pages/Statistics/ReportCenter'));
const SubsystemList = lazy(() => import('../pages/Subsystem/SubsystemList'));
const ElevatorSystem = lazy(() => import('../pages/Subsystem/ElevatorSystem'));
const HVACSystem = lazy(() => import('../pages/Subsystem/HVACSystem'));
const ParkingSystem = lazy(() => import('../pages/Subsystem/ParkingSystem'));
const UserList = lazy(() => import('../pages/UserManagement/UserList'));
const RoleList = lazy(() => import('../pages/UserManagement/RoleList'));
const SystemSettings = lazy(() => import('../pages/SystemManagement/SystemSettings'));
const SystemLogs = lazy(() => import('../pages/SystemManagement/SystemLogs'));

const Router: React.FC = () => {
  return (
    <BrowserRouter>
      <ErrorBoundary>
        <Suspense fallback={<PageLoader />}> 
          <Routes>
            {/* 登录页面 */}
            <Route path="/login" element={<Login />} />
            
            {/* 主布局路由 - 无需登录验证 */}
            <Route path="/" element={<BasicLayout />}>
              <Route index element={<Navigate to="/dashboard" replace />} />
              <Route path="dashboard" element={<Dashboard />} />
              <Route path="dashboard-3d" element={<Dashboard3DPage />} />
              <Route path="alarm/*" element={<Alarm />} />
              <Route path="profile" element={<Profile />} />
              <Route path="settings" element={<Settings />} />
              <Route path="equipment/list" element={<EquipmentList />} />
              <Route path="equipment/monitor" element={<EquipmentMonitor />} />
              <Route path="statistics/overview" element={<StatisticsOverview />} />
              <Route path="statistics/report" element={<ReportCenter />} />
              <Route path="subsystem/list" element={<SubsystemList />} />
              <Route path="subsystem/elevator" element={<ElevatorSystem />} />
              <Route path="subsystem/hvac" element={<HVACSystem />} />
              <Route path="subsystem/parking" element={<ParkingSystem />} />
              <Route path="user/list" element={<UserList />} />
              <Route path="user/roles" element={<RoleList />} />
              <Route path="system/settings" element={<SystemSettings />} />
              <Route path="system/logs" element={<SystemLogs />} />
            </Route>
            
            {/* 404页面 */}
            <Route path="*" element={<NotFound />} />
          </Routes>
        </Suspense>
      </ErrorBoundary>
    </BrowserRouter>
  );
};

export default Router;